package com.bw.ivideoproject.home

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.ItemSimplevideoBinding
import com.bw.ivideoproject.entity.SimpleVideoEntity
import com.bw.lib_imageutils.ImageLoaderManager
import com.bw.lib_imageutils.ImageOptions

class SimpleVideoAdapter(val context: Context) : RecyclerView.Adapter<SimpleVideoAdapter.ViewHolder>() {

    var data = mutableListOf<SimpleVideoEntity>()

    private var onItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener){
        this.onItemClickListener = onItemClickListener
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val binding = ItemSimplevideoBinding.bind(itemView)
    }

    fun refresh(list: List<SimpleVideoEntity>,isClear:Boolean){
        if(isClear){
            data.clear()
        }
        data.addAll(list)
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var view = LayoutInflater.from(parent.context).inflate(R.layout.item_simplevideo, parent,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val entity = data[position]
        holder.binding.tvName.text = entity.name
        holder.binding.tvDescription.text = entity.description
        val option = ImageOptions.Builder()
            .setPlaceImage(R.mipmap.ic_launcher)
            .build()
        ImageLoaderManager.getInstance().loadFileIntoImageView(context,entity.avatar_url,holder.binding.ivHead,option)
        val view = ImageView(context)
//        ImageLoaderManager.getInstance().loadFileIntoImageView(context,entity.videomainimag,view,option)
        Glide.with(context).load(entity.videomainimag).centerCrop().into(view)
        holder.binding.gsy.thumbImageView = view
        holder.binding.gsy.backButton.visibility = View.GONE
        holder.binding.gsy.setUp(entity.videopath,false,entity.title)

        holder.binding.root.setOnClickListener {
            onItemClickListener?.OnItemClick(entity)
        }
    }

    interface OnItemClickListener{
        fun OnItemClick(item:SimpleVideoEntity)
    }

}